package zut.edu.cn.concertticketmanagementsystem.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import zut.edu.cn.concertticketmanagementsystem.Dao.UserDao;
import zut.edu.cn.concertticketmanagementsystem.dto.CollectDTO;
import zut.edu.cn.concertticketmanagementsystem.entity.Collect;
import zut.edu.cn.concertticketmanagementsystem.entity.Ticket;
import zut.edu.cn.concertticketmanagementsystem.entity.User;
import zut.edu.cn.concertticketmanagementsystem.service.Collectservice;
import zut.edu.cn.concertticketmanagementsystem.service.Ticketservice;
import zut.edu.cn.concertticketmanagementsystem.service.UserService;

import java.util.ArrayList;
import java.util.List;

@Controller

@Slf4j
public class CollectController {
    @Autowired
    private Collectservice collectservice;
    @Autowired
    private Ticketservice ticketservice;
    @Autowired
    private UserDao userDao;

    @RequestMapping("/albums")
    public String collect( String username,Model model) {
        List<User>list1=new ArrayList<User>();
        list1=userDao.findUserByUsername(username);//姓名
        List<Collect> list=collectservice.findbyUserid(list1.get(0).getId().intValue());//id
        List<CollectDTO>collectDTOList=new ArrayList<CollectDTO>();
        for(int i=0;i<list.size();++i){
            Ticket ticket=new Ticket();
            ticket=ticketservice.findOneById(list.get(i).getPid());
//            ticket=ticketservice.findOneById(1);
            CollectDTO collectDTO=new CollectDTO();
            collectDTO.setUserid(list.get(i).getUserid());
            collectDTO.setPid(ticket.getT_id());
            collectDTO.setPrice(ticket.getT_price());
            collectDTO.setT_singername(ticket.getT_singername());
            collectDTO.setPlacename(ticket.getT_address());
            collectDTO.setDate(ticket.getT_date());
            collectDTO.setStarttime(ticket.getT_starttime());
            collectDTO.setEndtime(ticket.getT_endtime());
            collectDTOList.add(collectDTO);

        }
        for(int ii=0;ii<collectDTOList.size();++ii)
            System.out.println(collectDTOList.get(ii).toString());
        model.addAttribute("collect",collectDTOList);
        model.addAttribute("username",username);
        return "collect/albums";
    }
    @RequestMapping("/albumssave")
    public String savecollect(int tid,String name,Model model){
        List<User> user=new ArrayList<User>();
        Collect collect=new Collect();
        user=userDao.findUserByUsername(name);////name

        if(user.size()!=0)
            collect.setUserid(user.get(0).getId().intValue());
        else
        {
            model.addAttribute("username",name);
            return "collect/error";}
        collect.setPid(tid);////票id
        boolean flag=false;
        flag=collectservice.create(collect);

        if(flag)
        {
            model.addAttribute("username",name);
            return "collect/success";}
        else
        {
            model.addAttribute("username",name);
            return "collect/error";}
    }
    @RequestMapping("/albumsdelete")
    public String delete(int name, int id, Model model){

        Collect collect=new Collect();


        collect.setUserid(name);
        collect.setPid(id);
        collectservice.delete(collect);
        List<User> list=new ArrayList<User>();
        list=userDao.findUserById((long)name);
        model.addAttribute("username",list.get(0).getUsername());
        return "collect/delete";


    }
}
